<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<h1>司机监听测试页面——模拟司机客户端</h1>
<button onclick="setMessageContent('模拟司机客户端发送的请求')">模拟司机客户端发送的请求</button>
<div id="message">展示服务的推送过来消息的地方</div>
<button onclick="sourceClose()">关闭连接</button>
<button onclick="setMessageContent('模拟司机客户端发送的请求')">123</button>



<script>
    userId = "5";
    identity = 2;
    source = null;
    if (window.EventSource) {
        console.info("浏览器支持SSE");
        source = new EventSource('http://localhost:9000/connect?userId='+userId + "&identity="+identity);

        //监听服务的推送的消息
        source.addEventListener('message', function (e) {
            content = e.data
            console.info("消息内容："+content);

            setMessageContent(content)
        })

    } else {
        setMessageContent("此浏览器不支持")
    }

    function setMessageContent(content) {
        document.getElementById('message').innerHTML += (content + '<br/>');
    }
    function sourceClose() {
        console.info("close方法执行");
        source.close();
        httpRequest = new XMLHttpRequest();
        httpRequest.open("GET", "http://localhost:9000/close?userId="+userId  + "&identity="+identity);
        httpRequest.send();
        setMessageContent("已关闭连接");
    }

</script>
</body>
</html>